// src/pages/home/widgets/doctors/focusDoctor.tsx
import styles from "@styles/home.module.scss";
import {useFocusMutation} from "@store/apiSlice/profileApiSlice";
import {useTypedDispatch} from "@store/index";
import {toggleLikeFlag} from "@store/slices/doctorSlice";



interface Props {
    doctor: Doctor;
}

export default function FocusDoctor({ doctor }: Props) {
    //获取dispatch方法
    const dispatch = useTypedDispatch()

    // 用于实现关注操作
    const [focus, { isLoading }] = useFocusMutation();
    // 关注按钮点击事件的处理函数
    //关注按钮点击事件的处理函数
    const focusHandler = () =>{
        focus({type:'doc',id:doctor.id})
            .unwrap()
            .then(()=>dispatch(
                toggleLikeFlag({
                    id:doctor.id,
                    likeFlag:doctor.likeFlag===0?1:0,
                })
            ))
    }
    return (
        <button onClick={focusHandler} className={styles.focus_btn}>
            {isLoading ? "加载中" : doctor.likeFlag === 0 ? "+ 关注" : "已关注"}
        </button>
    );
}